#!/usr/bin/env python
import subprocess
import ConfigParser
import os
import datetime
import glob

fnull = open(os.devnull, 'w')

for dep in ['scanimage']:
    if subprocess.call(['which', dep], stdout=fnull, stderr=fnull) == 1:
        print 'Error: Could not find "%s" executable' % dep

'''
if len(argv) > 1:
    path = os.listdir(argv[1])
'''

config = ConfigParser.RawConfigParser()
config.read('options.txt')

#Make directory

d = datetime.datetime.today().date().strftime('%b-%d-%Y')
if not os.path.exists(d):
    os.makedirs(d)
os.chdir(d)

#Scanning

try:
    dpi = config.get('Options', 'dpi')
except:
    dpi = 600

os.system('scanimage --batch --format=tiff --source "ADF Front" --resolution %s --batch-start %i' % (dpi, max(map(int, list(entry[3:entry.index('.')] for entry in glob.glob('out*.tif')))) + 1 if len(glob.glob('out*.tif')) else 1))
#    pil = Image.open('out.tif') #ATTENTION
